﻿using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System;

namespace VPLink2013.Framework.Extensions
{
    public static class SPListExtension
    {
        public static string GetItemListLookUpFieldValueAsText(this SPListItem item, string fieldName)
        {
            SPFieldLookupValue lookupValue = new SPFieldLookupValue(item[fieldName] as string);
            if (lookupValue != null)
                return lookupValue.LookupValue;
            else return string.Empty;
        }

        public static int GetItemListLookUpFieldId(this SPListItem item, string fieldName)
        {
            SPFieldLookupValue lookupValue = new SPFieldLookupValue(item[fieldName] as string);
            if (lookupValue != null)
                return lookupValue.LookupId;
            else return -1;
        }

        private static string GetItemValue(this SPListItem item, string fieldName)
        {
            if (item == null)
            {
                return string.Empty;
            }
            string value = string.Empty;
            try
            {
                if (item[fieldName] == null)
                {
                    return string.Empty;
                }
                value = ((string)item[fieldName]).Trim();
            }
            catch (System.Exception ex)
            {
                ContextTrace.AddTrace(string.Format("Exception lors de la recherche du champ {0}", fieldName));
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            if (value == null)
            {
                value = string.Empty;
            }
            return value;
        }

        public static string GetItemListFieldValueAsText(this SPListItem item, string fieldName)
        {
            string value = string.Empty;
            try
            {
                SPFieldText textField = item.Fields[fieldName] as SPFieldText;
                if (textField != null)
                    value = textField.GetFieldValueAsText(item[fieldName]);
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(string.Format("Exception lors de la recherche du champ {0}", fieldName));
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            if (string.IsNullOrEmpty(value))
            {
                value = item.GetItemValue(fieldName);
            }
            return value;
        }

        public static string GetItemListFieldValueAsHtml(this SPListItem item, string fieldName)
        {
            string value = string.Empty;
            try
            {
                SPFieldMultiLineText textField = item.Fields[fieldName] as SPFieldMultiLineText;
                if (textField != null)
                    value = textField.GetFieldValueAsHtml(item[fieldName]);
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(string.Format("Exception lors de la recherche du champ {0}", fieldName));
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            if (string.IsNullOrEmpty(value))
            {
                value = item.GetItemValue(fieldName);
            }

            return value;
        }

        public static string GetItemListFieldValueAsUrlValue(this SPListItem item, string fieldName)
        {
            string value = string.Empty;
            try
            {
                SPFieldUrl urlField = item.Fields[fieldName] as SPFieldUrl;
                SPFieldUrlValue urlFieldVal = new SPFieldUrlValue(item[fieldName] as string);
                if (urlField != null)
                    value = urlFieldVal.Url;
            }
            catch(Exception ex)
            {
                ContextTrace.AddTrace(string.Format("Exception lors de la recherche du champ {0}", fieldName));
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            return value;
        }

        public static string GetItemListFieldValueAsDateTime(this SPListItem item, string fieldName)
        {
            string value = string.Empty;
            try
            {
                SPFieldDateTime dateField = item.Fields[fieldName] as SPFieldDateTime;
                if (dateField != null)
                    value = DateTime.Parse(dateField.GetFieldValueAsText(item[fieldName])).ToString("dd/MM/yyyy");
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(string.Format("Exception lors de la recherche du champ {0}", fieldName));
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            return value;
        }

        public static string GetItemListFieldValueAsUrlDescription(this SPListItem item, string fieldName)
        {
            string value = string.Empty;
            try
            {
                SPFieldUrl urlField = item.Fields[fieldName] as SPFieldUrl;
                SPFieldUrlValue urlFieldVal = new SPFieldUrlValue(item[fieldName] as string);
                if (urlField != null)
                    value = urlFieldVal.Description;
            }
            catch (Exception ex)
            {
                ContextTrace.AddTrace(string.Format("Exception lors de la recherche du champ {0}", fieldName));
                ContextTrace.AddTrace(ex.Message);
                ContextTrace.AddTrace(ex.StackTrace);
            }
            return value;
        }

        public static SPListItemCollection GetSPListItemCollectionWithTextFields(this SPList list, List<string> fields)
        {
            StringBuilder sbViewFields = list.BuildViewFields(fields);
            SPQuery query = new SPQuery()
            {
                ViewFields = sbViewFields.ToString()
            };
            return list.GetItems(query);
        }

        public static SPListItemCollection GetSPListItemCollectionWithTextFields(this SPList list, List<string> fields, SPQuery query)
        {
            StringBuilder sbViewFields = list.BuildViewFields(fields);
            query.ViewFields = sbViewFields.ToString();
            return list.GetItems(query);
        }

        private static StringBuilder BuildViewFields(this SPList list, List<string> fields)
        {
            StringBuilder sbViewFields = new StringBuilder();
            foreach (string fieldName in fields)
            {
                var textField = list.Fields[fieldName];
                if (textField != null)
                {
                    sbViewFields.AppendFormat(@"<FieldRef Name='{0}' />", textField.InternalName);
                }
            }
            return sbViewFields;
        }
    }
}
